<?php

class Controller_Specials extends Controller_TemplateNew
{
    public $_section;
    public $session;
    public $midle;
	public function  before() {
            parent::before();
            $this->db = Database::instance();
            $this->section = 'Special signs - ';

            $this->_section = 'specials';

            $this->template->description = $this->settings['description'];
            $this->template->keywords = $this->settings['keywords'];
            $this->template->linkSettings = $this->settings;

            $this->template->header->active='specials';
            $this->template->content = new View('specials/index');
            $this->template->content->downblock = new View('index/downblock');
            $this->template->content->downblock->formIdea = $this->formIdea;
            $this->midle = new View('index/midle');
            //session
            if (!isset($_SESSION['cart']['specials'])) {
                $_SESSION['cart']['specials'] = array();
            }
            
            //down Block
            $allImages = $this->mod->getImagesByItem('index');
            $bottomBlock = $this->mod->getItem(95, 'index');
            $this->template->content->downblock->bottomBlock = $bottomBlock;
            if (isset($allImages[$bottomBlock['id']])) {
                foreach ($allImages[$bottomBlock['id']] as $key=>$value) {
                    $allImages[$bottomBlock['id']][$key]['imgPath'] = $this->mod->imagePath($value,'box4');
                }
            }
            $bottomBlockCategory = $this->mod->getCategoryByAlias('images-79','images');
            $formConfig = $this->mod->getItemByAlias('9','images');
            $this->template->content->downblock->showForm = ($formConfig['checkbox7']==1) ? true : false;
            $bottomBlockItems = $this->mod->getCategoryItems($bottomBlockCategory['id'],'images','position');
            $tempImg = $this->mod->getImagesByItem('images','position');
            $tempAtt = $this->mod->getAttachmentsByItem('images');
            foreach ($tempImg as $v) {
                $bottomBlockItems[$v[0]['itemId']]['box4'] = $this->mod->imagePath($v[0],'box4');
            }
            foreach ($tempAtt as $v) {
                $bottomBlockItems[$v[0]['itemId']]['attacment'] = $this->mod->attachmentPath($v[0]);
            }
            $this->template->content->downblock->items = $bottomBlockItems;
            
            if (isset($allImages)) {
                $this->template->content->downblock->allImages = $allImages;
            }
            
            //midle Block
            $midle = $this->mod->getItem(202, 'index');
            if (isset($allImages[$midle['id']])) {
                foreach ($allImages[$midle['id']] as $key=>$value) {
                    $temp[$key] = $this->mod->imagePath($value,'box5');
                }
            }
            $this->midle->images = $temp;
            //
            
            $this->session = $_SESSION['cart'];
            
            $signsList = $this->mod->getCategories($this->_section, 'position');
            $this->template->content->signList = $signsList;
            $this->template->title = $this->section.$this->settings['title'];
                
            $this->addScript('/scripts/jquerytools/jquery.uniform.js');
            $this->addStyle('/styles/jquerytools/uniform.default.css');
	}
    
	public function actionIndex($page=1)
	{
            $this->template->content->activeCategoryId = null;
            
            $pages = new Pages($page);
            $pages->messageCount = $this->mod->getItemsCount($this->_section);
            $pages->limitPerPage = 11;
            $pages->link	     = Request::$controller.'index/';
            $pagesView = $pages->render();
            if($pages->messageCount > 0) {
                    $LIMIT = (!is_numeric($pages->currentPage)) ? '' : ' LIMIT ' . $pages->startId . ', ' . $pages->limitPerPage;
                    $allSigns = $this->db->query(
                            'SELECT * FROM `directoryItems` WHERE `section`=? AND `isCategory` = 0 ' . $LIMIT, array($this->_section))->fetchAll();
            } else {
                    $allSigns = false;
            }
            
            if ($allSigns) {
                foreach ($allSigns as $key=>$value) {
                    $image = $this->mod->getItemImages($value['id'], $this->_section);
                    if (isset($image)) {
                        foreach ($image as $k=>$v) {
                            $allSigns[$key]['catalogImage'] = $this->mod->imagePath($v,'sign');
                            $allSigns[$key]['fullImage'] = $this->mod->imagePath($v,'fullsize');
                        }                            
                    }
                }
                
            }
            $this->template->content->signsContent = new View('specials/allsigns');
            $this->template->content->signsContent->result = $allSigns;
            $this->template->content->signsContent->page = $pagesView;
            $this->template->content->signsContent->midleBlock = $this->midle;
	}
        public function actionSign($id=null, $page=1) {
            
            $item = $this->mod->getItem($id, $this->_section);
            $pages = new Pages($page);
            $pages->messageCount = $this->mod->getItemsCountByCategory($id, $this->_section);
            $pages->limitPerPage = 11;
            $pages->link	     = Request::$controller.'specials/'.$id.'/';
            $pagesView = $pages->render();
            if($pages->messageCount > 0) {
                    $LIMIT = (!is_numeric($pages->currentPage)) ? '' : ' LIMIT ' . $pages->startId . ', ' . $pages->limitPerPage;
                    $result = $this->db->query(
                            'SELECT * FROM `directoryItems` WHERE `parentId`=? AND `section`=? AND `isCategory` = 0 ORDER BY `position` ASC' . $LIMIT, array($id, $this->_section))->fetchAll();
            } else {
                    $result = false;
            }

            if ($result) {
                foreach ($result as $key=>$value) {
                    $image = $this->mod->getItemImages($value['id'], $this->_section);
                    if (isset($image)) {
                        foreach ($image as $k=>$v) {
                            $result[$key]['catalogImage'] = $this->mod->imagePath($v,'fullsize');
                        }                            
                    }
                }
            }
            $this->template->content->activeCategoryId = $id;
            $this->template->content->signsContent = new View('specials/allsigns');
            $this->template->content->signsContent->result = $result;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->page = $pagesView;
            $this->template->content->signsContent->midleBlock = $this->midle;
            
        }
        public function actionShow ($itemId) {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            $itemImage = $this->mod->getItemImages($item['id'], $this->_section);
            if (isset($itemImage)) {
                foreach ($itemImage as $key=>$value) {
                    $item['imgPath'] = $this->mod->imagePath($value,'preview');
                    $item['imgName'] = $value['name'];
                }
            }
            $itemAttachments = $this->mod-> getItemAttachments($item['id'], $this->_section);
            $attachmentShow = array();
            foreach ($itemAttachments as $key=>$value) {
                switch ($value['select1']) {
                    case 1: {$attachmentShow+= array('1'=>true);break;}
                    case 2: {$attachmentShow+= array('2'=>true);break;}
                    case 3: {$attachmentShow+= array('3'=>true);break;}
                    case 4: {$attachmentShow+= array('4'=>true);break;}
                    default :break;
                }
                switch ($value['select2']) {
                    case 5: {$attachmentShow+= array('5'=>true);break;}
                    case 6: {$attachmentShow+= array('6'=>true);break;}
                    default :break;
                }
            }
            $this->template->content->signsContent = new View('specials/show');
            $this->template->content->signsContent->showDialog = false;
            
            if (Request::$method == 'POST') {
                $attachment = $this->mod->getAttachmentBySelect($item['id'],$_POST['orientation'],$_POST['color']);
                $this->session['specials'] += array($item['id'] =>array('itemId' => $item['id'],
                                        'attachmentId' => $attachment['id']));
                $this->session += array('delivery' => $_POST['delivery'], 'deliveryCost' => $_POST['deliveryCost']);
                $_SESSION['cart'] = $this->session;
                $this->response->redirect('/specials/billing');
                $this->template->content->signsContent->showDialog = true;
            }
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->attachmentShow = $attachmentShow;
            $this->template->content->signsContent->midleBlock = $this->midle;
        }
        public function actionBilling () {
            foreach ($this->session['catalog'] as $k=>$v) {
                $itemId = $k;
                $attachmentId = $v['attachmentId'];
            }
            if (!isset($itemId)) $this->response->redirect('/');
            $item = $this->mod->getItem($itemId, $this->_section);
            $attachment = $this->mod->getAttachment($this->session['attachmentId'], $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            $itemImage = $this->mod->getItemImages($item['id'], $this->_section);
            if (isset($itemImage)) {
                foreach ($itemImage as $key=>$value) {
                    $item['imgPath'] = $this->mod->imagePath($value,'preview');
                    $item['imgName'] = $value['name'];
                }
            }
            //billingForm
            $billingForm = new Form('billing');
            $billingForm->template = 'billingForm';
            $billingForm->text('firstName',null,'First Name')->rule('required');
            $billingForm->text('secondName',null,'Second Name')->rule('required');
            $billingForm->text('adressLine1',null,'Adress Line1', 'new')->rule('required');
            $billingForm->text('adressLine2',null,'Adress Line2')->rule('required');
            $billingForm->text('city',null,'City')->rule('required');
            $billingForm->select('state', array_merge(array('' => 'State'), Text::$states), 'State')->rule('required')->bunched(true);
            $billingForm->text('zip',null,'Zip Code')->rule('required')->bunched(true);
            $billingForm->text('email',null,'Email')->rule('required')->rule('email');
            
            //billingForm
            $shippingForm = new Form('shipping');
            $shippingForm->template = 'billingForm';
            $shippingForm->text('firstName',null,'First Name')->rule('required');
            $shippingForm->text('secondName',null,'Second Name')->rule('required');
            $shippingForm->text('adressLine1',null,'Adress Line1', 'new')->rule('required');
            $shippingForm->text('adressLine2',null,'Adress Line2')->rule('required');
            $shippingForm->text('city',null,'City')->rule('required');
            $shippingForm->select('state', array_merge(array('' => 'State'), Text::$states), 'State')->rule('required')->bunched(true);
            $shippingForm->text('zip',null,'Zip Code')->rule('required')->bunched(true);
            $shippingForm->text('email',null,'Email')->rule('required')->rule('email');
            
            if (isset($this->session['shipping'])) {
                $shippingForm->loadValues($this->session['shipping']);
            }
            if (isset($this->session['billing'])) {
                $billingForm->loadValues($this->session['billing']);
                $billingForm->hidden('notReset', 1);
            }
            
            if (Request::$method == 'POST' && Request::$isAjax) {
//                print_r ($_POST);
//                exit;
                if ($billingForm->validate()) {
                    $valueBilling = $billingForm->getValues();
                    $valueShipping = $shippingForm->getValues();
                    foreach ($valueBilling as $k=>$v) {
                        $this->session['billing'][$k] = $valueBilling[$k];
                    }
                    $data = array('response'=>true);
                } 
                if ($shippingForm->validate()) {
                    $valueShipping = $shippingForm->getValues();
                    foreach ($valueShipping as $k=>$v) {
                        $this->session['shipping'][$k] = $valueShipping[$k];
                    }
                    $data = array('response'=>true);
                } else {
                    $data = array('response'=>false);
                    if ($billingForm->getErrors()) {
                        $data['billing'] = $billingForm->getErrors();
                    }
                    if ($shippingForm->getErrors()) {
                        $data['shipping'] = $shippingForm->getErrors();
                    }
                }
                $_SESSION['cart']['sign'] = $this->session;
                $this->autoRender = false;
                $this->response->setHeader('Content-Type', 'application/json');
                $this->response->body = json_encode($data);
                return;
            }
           
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('specials/billing');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->delivery = $this->session;
            $this->template->content->signsContent->attachment = $attachment;
            
            $this->template->content->signsContent->billingForm = $billingForm;
            $this->template->content->signsContent->shippingForm = $shippingForm;
            $this->template->content->signsContent->midleBlock = $this->midle;
        }
        
        
        public function actionFinalize ($itemId) {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('specials/finalize');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->buyItem = array($item);

        }
        public function actionThank () {
            $item = $this->mod->getItem($itemId, $this->_section);
            $parent = $this->mod->getItem($item['parentId'], $this->_section);
            
            
            $this->template->content->activeCategoryId = $item['parentId'];
            $this->template->content->signsContent = new View('specials/thank');
            $this->template->content->signsContent->parent = $parent;
            $this->template->content->signsContent->item = $item;
            $this->template->content->signsContent->buyItem = array($item);

        }
}